package com.qyhl.module_practice.team.detail;


import android.util.Log;

import com.google.gson.Gson;
import com.qyhl.module_practice.common.PracticeUrl;
import com.qyhl.module_practice.common.PracticeUtils;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.entity.civilized.PracticeTeamBean;

public class PracticeTeamDetailModel implements PracticeTeamDetailContract.PracticeTeamDetailModel {
    private PracticeTeamDetailPresenter mPresenter;

    PracticeTeamDetailModel(PracticeTeamDetailPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getDetail(String orgId, String volId) {
        EasyHttp.get(PracticeUrl.GET_PRACTICE_TEAM_DETAIL)
                .params("orgId", orgId)
                .params("volId", volId)
                .execute(new SimpleCallBack<PracticeTeamBean>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setError("暂无任何内容！");
                        } else {
                            mPresenter.setError("获取详情失败，请重新尝试！");
                        }
                    }

                    @Override
                    public void onSuccess(PracticeTeamBean practiceTeamBean) {
                        mPresenter.setDetail(practiceTeamBean);
                    }
                });
    }

    @Override
    public void joinTeam(String orgId, String volId) {
        EasyHttp.post(PracticeUrl.POST_PRACTICE_TEAM_JOIN)
                .params("orgId", orgId)
                .params("volId", volId)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.joinError("申请出错，请重新尝试！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.joinSuccess("申请成功，等待审核！");
                        } else {
                            mPresenter.joinError(PracticeUtils.getPracticeMessage(apiResult.getCode()));
                        }
                    }
                });
    }

    @Override
    public void exitTeam(String orgId, String volId) {

        EasyHttp.post(PracticeUrl.POST_PRACTICE_TEAM_EXIT)
                .params("orgId", orgId)
                .params("volId", volId)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.exitError("退出出错，请重新尝试！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.exitSuccess("您已成功退出该服务队！");
                        } else {
                            mPresenter.exitError(PracticeUtils.getPracticeMessage(apiResult.getCode()));
                        }
                    }
                });
    }
}
